package com.stylefeng.guns.rest.pojo;

import javax.mail.Address;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.util.*;

/**
 * @author yuzih
 * @create 2018-05-25 11:17
 * @desc 邮件业务用的对象
 **/
public class EmailObject {
    /**
     * 邮箱类型 如 qq google 等，用来决定使用什么配置文件
     */
    private String emailType;
    /**
     * 发件人地址
     */
    private String hostAddress;
    /**
     * 收件人地址（多个）
     */
    private List<String> targetAddresses;
    /**
     * 发件人密码
     */
    private String hostPassword;
    /**
     * 邮件标题
     */
    private String title;
    /**
     * 邮件内容
     */
    private String content;

    public EmailObject() {
    }

    /**
     * 使用map来初始化数据
     *
     * @param map 数据
     */
    public EmailObject(HashMap map) {
        this.setTitle((String) map.get("title"));
        this.setHostPassword((String) map.get("hostPassword"));
        this.setHostAddress((String) map.get("hostAddress"));
        this.setEmailType((String) map.get("emailType"));
        this.setContent((String) map.get("content"));
        String str = (String) map.get("targetAddresses");
        String[] strs = str.split(",");
        List<String> list = new ArrayList<>(Arrays.asList(strs));
        this.setTargetAddresses(list);
    }

    public String getEmailType() {
        return emailType;
    }

    public void setEmailType(String emailType) {
        this.emailType = emailType;
    }

    public String getHostAddress() {
        return hostAddress;
    }

    public void setHostAddress(String hostAddress) {
        this.hostAddress = hostAddress;
    }

    public List<String> getTargetAddresses() {
        return targetAddresses;
    }

    public void setTargetAddresses(List<String> targetAddresses) {
        this.targetAddresses = targetAddresses;
    }

    public String getHostPassword() {
        return hostPassword;
    }

    public void setHostPassword(String hostPassword) {
        this.hostPassword = hostPassword;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "EmailObject{" +
                "emailType='" + emailType + '\'' +
                ", hostAddress='" + hostAddress + '\'' +
                ", targetAddresses=" + targetAddresses +
                ", hostPassword='" + hostPassword + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }

    public Address[] getAddressArr(){
        Address[] address1 = new Address[this.targetAddresses.size()];
        try {
            for (int i = 0; i < address1.length; i++) {
                address1[i] = new InternetAddress(this.targetAddresses.get(i));
            }
        } catch (AddressException e) {
            e.printStackTrace();
        }
        return address1;
    }

}
